import Vue from 'vue'
import Router from 'vue-router'
import Register from './views/Register.vue'
import Login from './views/Login.vue'
import Index from './views/Index.vue'
import NotFound from './views/404.vue'
import MyIndex from "./components/MyIndex.vue"
import MyNote from "./components/MyNote.vue"
import MyEssay from "./components/MyEssay.vue"
import MyWorks from "./components/MyWorks.vue"

Vue.use(Router)

const router=new Router({
  mode: 'history',
  base: process.env.BASE_URL,
  routes: [
    //默认登录页面
    {
      path: '/',
      redirect:'/login'
    },
    //注册页面
    {
      path:'/register',
      name:'register',
      component:Register
    },
    //登录页面
    {
      path:'/login',
      name:'login',
      component:Login
    },
    //主页面
    {
      path:'/index',
      // name:'index',
      component:Index,
      children:[{
        path:'/',
        name:'myindex',
        component:MyIndex,//个人主页
      },
      {
        path:'/mynote',
        name:"mynote",
        component:MyNote,
      },
      {
        path:'/myessay',
        name:"myessay",
        component:MyEssay,
      },
      {
        path:'/myworks',
        name:"myworks",
        component:MyWorks,
      }
    ]
    },
    //404
    {
      path:'/*',
      name:'/404',
      component:NotFound
    }
    
  ]
})

//路由守卫
router.beforeEach((to,from,next)=>{
    const isLogin = localStorage.eleToken ? true : false;
    if(to.path=="/login" || to.path=="/register"){
      next();
    }
    else{
      isLogin ? next():next("/login");
    }
})


export default router;